<?php 
		namespace Admin\Model;
		use Think\Model\RelationModel;
		class PositionModel extends RelationModel	{
			protected $tableName = 'position';
			protected $_link = array(       
					'position_data' => array(   
							 'mapping_type'  => self::HAS_MANY,  
							  'class_name'    => 'position_data',   
								 'foreign_key'   => 'p_id', 
							   'mapping_name'  => 'data', 
							  // 'mapping_order' => 'listorder desc',    // 定义更多的关联属性    ……
							   ),
												   );
			
			//查询一条数据
			public function getOne($id){
				return $this->relation(true)->find($id);
			}
			
			public function positionSave($arr){
				 
				
				
			}
			
			/**
			 *获取文章的推荐信息 
			 */
			public function selectPositionInfo($model=0,$cate_id=0,$id=0){
				$sql="SELECT
						a.*,
						b.data_id as flag
						FROM
						think_position as a LEFT JOIN (select * from think_position_data where data_id='$id' and cate_id='$cate_id') as b on b.p_id=a.id
						where model_id in($model,0)
						GROUP BY
						a.id
						ORDER BY
						a.id DESC";
						$list=$this->query($sql);      //获取推进位数据
					return $list;
						
			}
			
			/**
			 * 获取推荐位数据
			 */
			public function getPositionData($pid,$num,$catid){
				$p_data = $this->getOne($pid);
				$model = D('Admin/Moxing');
				$model = $model->getModel();
				$table = $model[$p_data['model_id']]['table'];
				$db = D($table);
				$str1="";
				$str="";
				foreach ($p_data['data'] as $r){
					$str = $str.$r['data_id'].",";
				}
				$str = substr($str, 0,strlen($str)-1);
				
				if($catid!=0){
					$str1 =cateSon($catid);
					$str1 = " cate_id in($catid)";
				}
				return $db->where($str1)->order("rand()")->limit(0,$num)->select();
			}
			
		}


?>